<script type="text/javascript" src="<?php echo $this->baseUrl(); ?>/js/jquery/jstree/jquery.jstree.js"></script>
<link rel="stylesheet" href="<?php echo $this->baseUrl(); ?>/js/jquery/jstree/themes/default/style.css" media="screen" />

<div class="ev-formulario">

    <div class="tit-page">
        <h2>Proposta Comercial</h2>
    </div>
<?php if ($this->row->id_proposta) : ?>
            <a class="ui-state-default ui-corner-all" href="<?php echo $this->baseUrl() ?>/contrato/proposta/gerar-pdf/id_proposta/<?php echo $this->row->id_proposta; ?>">
                <span class="ui-icon ui-icon-check"></span>
                Gerar PDF
            </a>
            <a class="ui-state-default ui-corner-all" href="<?php echo $this->baseUrl() ?>/contrato/proposta/gerar-pdf/id_proposta/<?php echo $this->row->id_proposta; ?>">
                <span class="ui-icon ui-icon-check"></span>
                Enviar
            </a>
<?php endif; ?>
    <form action="<?php echo $this->baseUrl() ?>/contrato/proposta/gravar" method="post" name="formulario" id="formulario">
        <input type="hidden" name="id_proposta" id="id_proposta" value="<?php echo $this->row->id_proposta ?>" />
        <input type="hidden" name="st_codigo" id="st_codigo" value="<?php echo $this->row->st_codigo ?>" />
        <fieldset>
            <div id="div-esquerda">
                <div class="ev-campo">
                    <label for="st_codigo"><?php echo $this->translate( "st_codigo"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                    <b><?php echo $this->row->st_codigo; ?></b>&nbsp;
                </div>

                <div class="ev-campo">
                    <label for="fk_cliente"><?php echo $this->translate( "fk_cliente"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                    <?php echo $this->formSelect( "fk_cliente", $this->row->fk_cliente, array( "id" => "fk_cliente", "title" => $this->translate("fk_cliente"), "class" => "required"), $this->aReferencias["fk_cliente"] ); ?>
                </div>

                <div class="ev-campo">
                    <label for="fk_contato"><?php echo $this->translate( "fk_contato"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                    <?php echo $this->formSelect( "fk_contato", $this->row->fk_contato, array( "id" => "fk_contato", "title" => $this->translate("fk_contato"), "class" => "required"), $this->aReferencias["fk_contato"] ); ?>
                </div>

                <div class="ev-campo">
                    <label for="fk_usuario_responsavel"><?php echo $this->translate( "fk_usuario_responsavel"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                    <?php echo $this->formSelect( "fk_usuario_responsavel", $this->row->fk_usuario_responsavel, array( "id" => "fk_usuario_responsavel", "title" => $this->translate("fk_usuario_responsavel"), "class" => "required"), $this->aReferencias["fk_usuario_responsavel"] ); ?>
                </div>


                <div class="ev-campo">
                    <label for="st_nome"><?php echo $this->translate( "st_nome"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                    <input class="text ui-widget-content ui-corner-all required" type="text" name="st_nome" id="st_nome" value="<?php echo $this->row->st_nome; ?>" title="<?php echo $this->translate( "st_nome"); ?>"  maxlength="150" />
                </div>

                <div class="ev-campo">
                    <label for="qt_dias_validade"><?php echo $this->translate( "qt_dias_validade"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                    <input class="text ui-widget-content ui-corner-all required" type="text" name="qt_dias_validade" id="qt_dias_validade" value="<?php echo $this->row->qt_dias_validade; ?>" title="<?php echo $this->translate( "qt_dias_validade"); ?>"   />
                </div>
            </div>

            <div id="div-direita">
                <fieldset>
                    <legend>Cálculos</legend>
                    <div class="ev-campo">
                        <label for="vl_custo"><?php echo $this->translate( "vl_custo"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                        <b><?php echo $this->row->vl_custo; ?></b>&nbsp;
                    </div>

                    <div class="ev-campo">
                        <label for="vl_total"><?php echo $this->translate( "vl_total"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                        <input class="text ui-widget-content ui-corner-all required" type="text" name="vl_total" id="vl_total" value="<?php echo $this->row->vl_total; ?>" title="<?php echo $this->translate( "vl_total"); ?>"   />
                    </div>

                    <div class="ev-campo">
                        <label for="qt_parcelas"><?php echo $this->translate( "qt_parcelas"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                        <input class="text ui-widget-content ui-corner-all required" type="text" name="qt_parcelas" id="qt_parcelas" value="<?php echo $this->row->qt_parcelas; ?>" title="<?php echo $this->translate( "qt_parcelas"); ?>"   />
                    </div>
                </fieldset>
            </div>
<?php /*
            <div class="ev-campo">
                <label for="st_path_assinado"><?php echo $this->translate( "st_path_assinado"); ?>: </label>
                <input class="text ui-widget-content ui-corner-all" type="text" name="st_path_assinado" id="st_path_assinado" value="<?php echo $this->row->st_path_assinado; ?>" title="<?php echo $this->translate( "st_path_assinado"); ?>"  maxlength="150" />
            </div>

            <div class="ev-campo">
                <label for="tx_conteudo"><?php echo $this->translate( "tx_conteudo"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                <textarea class="text ui-widget-content ui-corner-all required" name="tx_conteudo" id="tx_conteudo" title="<?php echo $this->translate( "tx_conteudo"); ?>" ><?php echo $this->row->tx_conteudo ?></textarea>
            </div>

            <div class="ev-campo">
                <label for="cs_situacao"><?php echo $this->translate( "cs_situacao"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                <input class="text ui-widget-content ui-corner-all required" type="text" name="cs_situacao" id="cs_situacao" value="<?php echo $this->row->cs_situacao; ?>" title="<?php echo $this->translate( "cs_situacao"); ?>"   />
            </div>

            <div class="ev-campo">
                <label for="dt_cadastro"><?php echo $this->translate( "dt_cadastro"); ?>&nbsp;<span class="campoObrigatorio">*</span>: </label>
                <input class="text ui-widget-content ui-corner-all required data" type="text" name="dt_cadastro" id="dt_cadastro" value="<?php echo $this->dateFormat($this->row->dt_cadastro); ?>" title="<?php echo $this->translate( "dt_cadastro"); ?>"   />
            </div>
*/ ?>
        </fieldset>

<?php if ($this->row->id_proposta) : ?>
        <fieldset>
            <legend>Entregas</legend>
            <div class="ev-option-buttons">
                <a class="ev-dialog-form ui-state-default ui-corner-all"
                    href="<?php echo $this->baseUrl(); ?>/contrato/proposta/formulario-entrega/fk_proposta/<?php echo $this->row->id_proposta ?>">
                    <span class="ui-icon ui-icon-plusthick"></span>
                    Nova Entrega
               </a>
           </div>

            <div>
                <div id="div-esquerda">
                    <fieldset>
                        <legend>Entregas</legend>
                        <div id="tree">
                            <ul class="tree treeview" id="root-treeview">
                                <?php echo $this->treeView($this->rowSetEntregas, $this->baseUrl() . '/contrato/proposta/formulario-entrega', 'id_entrega', 'st_codigo', 'st_nome'); ?>
                            </ul>
                    </fieldset>
                </div>
                <div id="div-direita">
                    <fieldset>
                        <legend>Tipo de tarefa</legend>
                        <div>
                            <ul>
                                <?php foreach ($this->rowSetTarefaTipo as $rowTipo) : ?>
                                <li><?php echo $rowTipo->st_nome; ?></li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    </fieldset>
                </div>
            </div>

        </fieldset>
<?php endif; ?>
        <div class="botoesAcao">
            <button type="submit" name="botao-salvar" id="botao-salvar">Salvar</button>
            <button type="button" name="botao-cancelar" id="botao-cancelar" action="<?php echo $this->baseUrl() ?>/contrato/proposta">Cancelar</button>
        </div>
    </form>
</div>

<script type="text/javascript">
    $().ready(function() {
        $("form").validate();
        $('#cancelar').click(function(){
            window.location = "<?php echo $this->baseUrl() ?>/contrato/proposta";
        });

        $("#tree a").addClass("ev-dialog-form");

    	$("#tree")
    		.bind("before.jstree", function (e, data) {
    			$("#alog").append(data.func + "<br />");
    		})
            .bind("loaded.jstree", function (e, data) {
                data.inst.open_all(-1); // -1 opens all nodes in the container
            })
    		.jstree({
    			// List of active plugins
    			"plugins" : [
    				"themes","html_data"
                    /* ,"ui","crrm","dnd","contextmenu","types", */
    			],

    			// I usually configure the plugin that handles the data first
    			// This example uses JSON as it is most common
    			"json_data" : {
    				// This tree is ajax enabled - as this is most common, and maybe a bit more complex
    				// All the options are almost the same as jQuery's AJAX (read the docs)
    				"ajax" : {
    					// the URL to fetch the data
    					"url" : "/static/v.1.0pre/_demo/server.php",
    					// the `data` function is executed in the instance's scope
    					// the parameter is the node being loaded
    					// (may be -1, 0, or undefined when loading the root nodes)
    					"data" : function (n) {
    						// the result is fed to the AJAX request `data` option
    						return {
    							"operation" : "get_children",
    							"id" : n.attr ? n.attr("id").replace("node_","") : 1
    						};
    					}
    				}
    			},
    			// Using types - most of the time this is an overkill
    			// read the docs carefully to decide whether you need types
    			"types" : {
    				// I set both options to -2, as I do not need depth and children count checking
    				// Those two checks may slow jstree a lot, so use only when needed
    				"max_depth" : -2,
    				"max_children" : -2,
    				// I want only `drive` nodes to be root nodes
    				// This will prevent moving or creating any other type as a root node
    				"valid_children" : [ "drive" ],
    				"types" : {
    					// The default type
    					"default" : {
    						// I want this type to have no children (so only leaf nodes)
    						// In my case - those are files
    						"valid_children" : "none",
    						// If we specify an icon for the default type it WILL OVERRIDE the theme icons
    						"icon" : {
    							"image" : "/static/v.1.0pre/_demo/file.png"
    						}
    					},
    					// The `folder` type
    					"folder" : {
    						// can have files and other folders inside of it, but NOT `drive` nodes
    						"valid_children" : [ "default", "folder" ],
    						"icon" : {
    							"image" : "/static/v.1.0pre/_demo/folder.png"
    						}
    					},
    					// The `drive` nodes
    					"drive" : {
    						// can have files and folders inside, but NOT other `drive` nodes
    						"valid_children" : [ "default", "folder" ],
    						"icon" : {
    							"image" : "/static/v.1.0pre/_demo/root.png"
    						},
    						// those prevent the functions with the same name to be used on `drive` nodes
    						// internally the `before` event is used
    						"start_drag" : false,
    						"move_node" : false,
    						"delete_node" : false,
    						"remove" : false
    					}
    				}
    			},
    			// UI & core - the nodes to initially select and open will be overwritten by the cookie plugin

    			// the UI plugin - it handles selecting/deselecting/hovering nodes
    			"ui" : {
    				// this makes the node with ID node_4 selected onload
    				"initially_select" : [ "node_4" ]
    			},
    			// the core plugin - not many options here
    			"core" : {
    				// just open those two nodes up
    				// as this is an AJAX enabled tree, both will be downloaded from the server
    				"initially_open" : [ "node_2" , "node_3" ]
    			}
    		});
        /*
    		.bind("create.jstree", function (e, data) {
    			$.post(
    				"/static/v.1.0pre/_demo/server.php",
    				{
    					"operation" : "create_node",
    					"id" : data.rslt.parent.attr("id").replace("node_",""),
    					"position" : data.rslt.position,
    					"title" : data.rslt.name,
    					"type" : data.rslt.obj.attr("rel")
    				},
    				function (r) {
    					if(r.status) {
    						$(data.rslt.obj).attr("id", "node_" + r.id);
    					}
    					else {
    						$.jstree.rollback(data.rlbk);
    					}
    				}
    			);
    		})
    		.bind("remove.jstree", function (e, data) {
    			data.rslt.obj.each(function () {
    				$.ajax({
    					async : false,
    					type: 'POST',
    					url: "/static/v.1.0pre/_demo/server.php",
    					data : {
    						"operation" : "remove_node",
    						"id" : this.id.replace("node_","")
    					},
    					success : function (r) {
    						if(!r.status) {
    							data.inst.refresh();
    						}
    					}
    				});
    			});
    		})
    		.bind("rename.jstree", function (e, data) {
    			$.post(
    				"/static/v.1.0pre/_demo/server.php",
    				{
    					"operation" : "rename_node",
    					"id" : data.rslt.obj.attr("id").replace("node_",""),
    					"title" : data.rslt.new_name
    				},
    				function (r) {
    					if(!r.status) {
    						$.jstree.rollback(data.rlbk);
    					}
    				}
    			);
    		})
    		.bind("move_node.jstree", function (e, data) {
    			data.rslt.o.each(function (i) {
    				$.ajax({
    					async : false,
    					type: 'POST',
    					url: "/static/v.1.0pre/_demo/server.php",
    					data : {
    						"operation" : "move_node",
    						"id" : $(this).attr("id").replace("node_",""),
    						"ref" : data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id").replace("node_",""),
    						"position" : data.rslt.cp + i,
    						"title" : data.rslt.name,
    						"copy" : data.rslt.cy ? 1 : 0
    					},
    					success : function (r) {
    						if(!r.status) {
    							$.jstree.rollback(data.rlbk);
    						}
    						else {
    							$(data.rslt.oc).attr("id", "node_" + r.id);
    							if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
    								data.inst.refresh(data.inst._get_parent(data.rslt.oc));
    							}
    						}
    						$("#analyze").click();
    					}
    				});
    			});
    		});
*/
    	});

</script>